﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class DanhSachQuyTrinhMuaHang : UserControl
    {
        public DanhSachQuyTrinhMuaHang()
        {
            InitializeComponent();
        }

        private void DanhSachQuyTrinhMuaHang_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            dgvQuyTrinhMuaHang.Rows.Clear();

            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            var listQuyTrinh = from qt in dc.QuyTrinhMuaHangs
                               where qt.deleted == false
                               orderby qt.ID descending
                               select qt;

            int count = 1;
            foreach (_DB.QuyTrinhMuaHang qt in listQuyTrinh)
            {
                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvQuyTrinhMuaHang);
                r.Cells[0].Value = qt.ID;
                r.Cells[1].Value = qt.MaPhieuDeNghiMuaHang;
                r.Cells[2].Value = qt.PhieuDeNghiMuaHang.NgayTao;
                r.Cells[3].Value = qt.TrangThaiQuyTrinhMuaHang.MoTa;

                dgvQuyTrinhMuaHang.Rows.Add(r);
            }
        }

        private void dgvQuyTrinhMuaHang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            // check
            if (dgvQuyTrinhMuaHang.SelectedRows.Count <= 0)
                return;
            if (MessageBox.Show("Chắc chắn", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // go
            string maPhieuDeNghi = dgvQuyTrinhMuaHang.SelectedRows[0].Cells[1].Value as string;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuDeNghiMuaHang == maPhieuDeNghi);
            qt.deleted = true;
            dc.SubmitChanges();
        }

        private void dgvQuyTrinhMuaHang_DoubleClick(object sender, EventArgs e)
        {
            if (dgvQuyTrinhMuaHang.SelectedRows.Count <= 0)
                return;

            int ID = (int) dgvQuyTrinhMuaHang.SelectedRows[0].Cells[0].Value;
            OtherForms.XemQuyTrinh f = new OtherForms.XemQuyTrinh(ID);
            f.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // check
            if (dgvQuyTrinhMuaHang.SelectedRows.Count <= 0)
                return;

            // check trang thai
            int ID = (int)dgvQuyTrinhMuaHang.SelectedRows[0].Cells[0].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.ID == ID);
            if (qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.DA_DUYET_PHIEU_THANH_TOAN)
                return;

            if (MessageBox.Show("Chắc chắn", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // go
            qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.KET_THUC;
            dc.SubmitChanges();
            loadData();
        }
    }
}
